/*****************************************************************************/
/*!	\file		UserInput.c
 *	\brief		C Source code file for the YX-PIS Example
 *	\par		Dalian Yunxing Tech Co., Ltd.
 *
 *				Dalian, China
 *				Phone   : +86 (411) 8825 4852
 *				Email   : yx@yunxing.tech
 */
/*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "UserInput.h"
#include "ExampleTypes.h"
#include "LocalData.h"
#include "IEC61850API.h"

enum UserCommands GetServerCommandFromUser()
{
	enum UserCommands returnVal = COMMAND_NONE;
	char userInput[256] = { 0 }; //Buffer to hold the user input

	if (fgets(userInput, sizeof(userInput), stdin) != NULL) {
		switch (userInput[0]) {
		case 'x':
		case 'X':
			returnVal = COMMAND_EXIT;
			break;
		case '0':
			returnVal = COMMAND_MMS_GET_CONNECTED_SERVER_LIST;
			break;
		case '1':
			returnVal = COMMAND_UPDATE_MMXU_PHSA;
			break;
		case '2':
			returnVal = COMMAND_UPDATE_MMXU_PHSB;
			break;
		case '3':
			returnVal = COMMAND_UPDATE_MMXU_PHSC;
			break;
		case '4':
			returnVal = COMMAND_UPDATE_PDIF_XVAL;
			break;
		case '5':
			returnVal = COMMAND_UPDATE_PDIF_YVAL;
			break;
		case '6':
			returnVal = COMMAND_UPDATE_TTNS_TNSSV1;
			break;
		case '7':
			returnVal = COMMAND_UPDATE_TTNS_TNSSV2;
			break;
		case '8':
			returnVal = COMMAND_UPDATE_TTNS_TNSSV3;
			break;
		case 't':
			returnVal = COMMAND_INNER_UNIT_TEST;
			break;
		}
	}

	return returnVal;
}

enum UserCommands GetClientCommandFromUser()
{
	enum UserCommands returnVal = COMMAND_NONE;
	char userInput[256] = { 0 }; //Buffer to hold the user input
	setbuf(stdin, NULL);
	if (fgets(userInput, sizeof(userInput), stdin) != NULL) {
		switch (userInput[0]) {
		case 'x':
		case 'X':
			returnVal = COMMAND_EXIT;
			break;
		case '0':
			returnVal = COMMAND_MMS_GET_CONNECTED_SERVER_LIST;
			break;
		case '1':
			returnVal = COMMAND_PRINT_REPORT_MENU;
			break;
		case '2':
			returnVal = COMMAND_PRINT_CONTROL_MENU;
			break;
		case '3':
			returnVal = COMMAND_PRINT_FILE_MENU;
			break;
		case '4':
			returnVal = COMMAND_PRINT_LOG_MENU;
			break;
		case '5':
			returnVal = COMMAND_PRINT_SETTING_GROUP_MENU;
			break;
		case '6':
			returnVal = COMMAND_PRINT_DATA_ACCESS_MENU;
			break;
		case 't':
			returnVal = COMMAND_INNER_UNIT_TEST;
			break;
		default:
			break;
		}
	}

	return returnVal;
}

Boolean GetBooleanFromUser()
{
	Boolean returnVal = FALSE;
	char userInput[256] = { 0 }; //Buffer to hold the user input

	printf("Enter Boolean Value, press T for TRUE or press F for FALSE: ");
	setbuf(stdin, NULL);
	if (fgets(userInput, sizeof(userInput), stdin) != NULL) {
		switch (userInput[0]) //Base the return val on the input
		{
		case '0':
		case 'f':
		case 'F':
			returnVal = FALSE;  //0 or f or F means FALSE
			break;
		case '1':
		case 't':
		case 'T':
			returnVal = TRUE;  //1 or t or T means TRUE
			break;
		}
	}

	return returnVal;
}

S32 GetInteger32FromUser()
{
	S32 returnVal = 0;

	printf("Enter an S32 value: ");

#ifdef __PARADIGM__
	scanf("%ld", &returnVal); //On BECK an S32 is a long int so %ld is needed
#else
	scanf("%d", &returnVal);
#endif
	return returnVal;
}

S32 GetCommIndexFromUser()
{
	S32 returnVal = 0;

	printf("Enter command index: ");

#ifdef __PARADIGM__
	scanf("%ld", &returnVal); //On BECK an S32 is a long int so %ld is needed
#else
	scanf("%d", &returnVal);
#endif
	return returnVal;
}

S8 GetIndexForArray()
{
	S8 returnVal = 0;
	S32 tempInt32 = 0;

	printf("Enter the index for the Array [0 - %d]: ", SIZE_OF_PDIF_ARRAY - 1);

	scanf("%d", &tempInt32); //%d expects a 32 bit Integer

	returnVal = (S8)tempInt32; //Type cast the S32 to an INT8

	return returnVal;
}

Float32 GetFloat32FromUser()
{
	Float32 returnVal = 0;

	printf("Enter a Float32 value: ");

	scanf("%f", &returnVal);

	return returnVal;
}

unsigned int GetIndexFromUser()
{
	unsigned int returnValue = 0xFFFF;
	char buffer[BUFSIZ];
	printf("Enter the index: ");
	setbuf(stdin, NULL);
	fgets(buffer, sizeof(buffer), stdin);
	sscanf(buffer, "%u", &returnValue);

	return returnValue;
}

void GetFileNameFromUser(char* messageString, char* userInput)
{
	int returnError = 0;
	int ch;

	if (!messageString || !userInput) {
		returnError = -1;
	}

	// Get user input
	if (returnError == 0) {
		printf("%s", messageString); //print the message
		fflush(stdout);
		setbuf(stdin, NULL);
		if (fgets(userInput, 256, stdin) == NULL) //get user input string
		{
			returnError = -1;
		}
		//sscanf(line, "%255s", userInput); //get string from input
	}

	// If the input string is too long, print out error message
	// It also flush the reset so that it won't affect the next call
	if (returnError == 0) {
		if (userInput[strlen(userInput) - 1] != '\n') {
			// Read the remaining characters
			while (((ch = getchar()) != '\n') && (ch != EOF)) {
				returnError = -1;
			};

			if (returnError != 0) {
				printf("Filename must be shorter than 255 characters. Please try again.\n");
			}
		}
	}

	if (returnError == 0) {
		if (userInput[0] == '\n') {
			strcpy(userInput, "");
		} else {
			userInput[strlen(userInput) - 1] = '\0';
		}
	}
}

IEC61850_TimeStamp* GetIEC61850TimeFromUser()
{
	IEC61850_TimeStamp* timestamp = NULL;

	char userInput[64] = { 0 };
	char* strDateTime = userInput;
	setbuf(stdin, NULL);
	if (fgets(userInput, sizeof(userInput), stdin) != NULL) {
		int nLen = 0;
		nLen = (int)strlen(strDateTime);
		if (nLen >= 14 && nLen < sizeof(userInput)) {
			char year[5] = { 0 };
			char month[3] = { 0 };
			char day[3] = { 0 };
			char hour[3] = { 0 };
			char minute[3] = { 0 };
			char sec[3] = { 0 };
			char millisec[4] = { 0 };

			memcpy(year, strDateTime, 4);
			memcpy(month, strDateTime += 4, 2);
			memcpy(day, strDateTime += 2, 2);
			memcpy(hour, strDateTime += 2, 2);
			memcpy(minute, strDateTime += 2, 2);
			memcpy(sec, strDateTime += 2, 2);
			timestamp = calloc(1, sizeof(IEC61850_TimeStamp));
			if (timestamp) {
				IEC61850_GetIEC61850TimeFromDate(timestamp, atoi(month), atoi(day), atoi(year), atoi(hour), atoi(minute), atoi(sec), atoi(millisec));
			}
		}
	}
	return timestamp;
}